package com.example.yk

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.yk.adapter.BrandAdapter
import com.example.yk.adapter.ConditionAdapte
import com.example.yk.databinding.ActivityMoreBinding
import com.example.yk.entity.BrandEntity
import com.example.yk.entity.ConditionEntity
import com.example.zk3.mvvm.BaseActivity

@Route(path = "/app/MoreActivity")
class MoreActivity : BaseActivity<ActivityMoreBinding,MoreViewModel>() {
    val list = mutableListOf<BrandEntity>()
    val map = mutableMapOf<String,Any>()
    lateinit var brandAdapter: BrandAdapter

    val goodslist = mutableListOf<ConditionEntity>()
    lateinit var conditionAdapter: ConditionAdapte
    override fun initData() {
        viewModel.brandSuccess.observe(this){
            if(it.success){
                list.clear()
                list.addAll(it.datas)
                brandAdapter.notifyDataSetChanged()
            }
        }
        viewModel.success.observe(this){
            if(it.success){
                goodslist.clear()
                goodslist.addAll(it.rows)
                conditionAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun initView() {
        brandAdapter = BrandAdapter(list)
        binding.pinpai.adapter = brandAdapter
        binding.pinpai.layoutManager = GridLayoutManager(this,3)
        viewModel.brand()

        conditionAdapter = ConditionAdapte(goodslist)
        binding.shangpin.adapter = conditionAdapter
        binding.shangpin.layoutManager = LinearLayoutManager(this)

        map.clear()

        map["sort"] = "desc"
        map["sortKey"] = "soldNumSort"
        viewModel.conditon(map)

        binding.tvBrand.setOnClickListener {
            map.clear()

            map["sort"] = "desc"
            map["sortKey"] = "soldNumSort"
            map["brandId"] = 1066
            viewModel.conditon(map)
        }
        binding.tvSoldNum.setOnClickListener {
            map.clear()

            map["sort"] = "desc"
            map["sortKey"] = "soldNumSort"
            viewModel.conditon(map)
        }
        binding.tvNewGoods.setOnClickListener {
            map.clear()

            map["sort"] = "desc"
            map["sortKey"] = "newGoodsSort"
            viewModel.conditon(map)
        }
        binding.tvPrice.setOnClickListener {
            binding.llPrice.isVisible = !binding.llPrice.isVisible
        }
        binding.btnSure.setOnClickListener {
            map.clear()

            map["sort"] = "asc"
            map["sortKey"] = "priceSort"
            map["maxMoney"] =100
            map["minMoney"] =10
            viewModel.conditon(map)
            binding.llPrice.isVisible = false
        }

    }

    override fun getLayout(): Int  = R.layout.activity_more

    override fun getViewModel(): Class<MoreViewModel>  = MoreViewModel::class.java

}